
from srbpy import Align
from PyAngle import Angle
from ezdxf.math import Vec2
import pandas as pd


class Plinth:
    def __init__(self, id: int, side: str, type, center: 'Vec2', axis_x: 'Vec2', axis_y: 'Vec2'):
        self.id = id
        self.side = side
        self.type = type
        self.center = center
        self.axis_x = axis_x
        self.axis_y = axis_y
        if self.type == 'TYPE 2B':
            self.w = 0.6
            self.l = 0.5
        else:
            raise Exception()
        self.coords = [
            [(center + axis_x * 0.5 * self.l + axis_y * 0.5 * self.w).x, (center + axis_x * 0.5 * self.l + axis_y * 0.5 * self.w).y],
            [(center - axis_x * 0.5 * self.l + axis_y * 0.5 * self.w).x, (center - axis_x * 0.5 * self.l + axis_y * 0.5 * self.w).y],
            [(center - axis_x * 0.5 * self.l - axis_y * 0.5 * self.w).x, (center - axis_x * 0.5 * self.l - axis_y * 0.5 * self.w).y],
            [(center + axis_x * 0.5 * self.l - axis_y * 0.5 * self.w).x, (center + axis_x * 0.5 * self.l - axis_y * 0.5 * self.w).y],
        ]


class CBInfo:

    def __init__(self, name: str, station: float, width: float, lw: float, dist: float,
                 plinth_list_A, plinth_list_B, ML: 'Align',
                 angle: 'Angle' = Angle.from_degrees(90.0)):
        self.name = name
        self.station = station
        self.width = width
        self.left_width = lw
        self.dist = dist
        self.ml = ML
        self.cc = Vec2(*self.ml.get_coordinate(station))
        self.ax = Vec2(*ML.get_direction(station))
        self.ay = self.ax.rotate_deg(90.0)

        self.plinth_A = []
        self.plinth_B = []
        left_pt = self.cc + self.ay * lw - self.ax * dist
        for ii, dy in enumerate(plinth_list_A):
            left_pt = left_pt - self.ay * dy
            self.plinth_A.append(Plinth(ii, 'A', 'TYPE 2B', left_pt, self.ax, self.ay))
        left_pt = self.cc + self.ay * lw + self.ax * dist
        for ii, dy in enumerate(plinth_list_B):
            left_pt = left_pt - self.ay * dy
            self.plinth_B.append(Plinth(ii, 'B', 'TYPE 2B', left_pt, self.ax, self.ay))
        pass

    def get_dataframe(self):
        out = []
        elem = []
        for plt in self.plinth_A:
            for ii, xy in enumerate(plt.coords):
                elem = [self.name, 'A', str(plt.id + 1) + "-" + str(ii + 1), *xy]
                out.append(elem)
        for plt in self.plinth_B:
            for ii, xy in enumerate(plt.coords):
                elem = [self.name, 'B', str(plt.id + 1) + "-" + str(ii + 1), *xy]
                out.append(elem)
        df = pd.DataFrame(data=out, columns=['Pier', 'Side', 'PlinthNo', 'East', 'North', ])
        return df


if __name__ == "__main__":
    M = Align("ML", './data/M')

    with open("垫石信息.txt", 'r', encoding='utf8') as fid:
        text = fid.readlines()

    input_list = []
    inputs = []
    for line in text:
        line = line.strip()
        if line.startswith("#") or line.startswith('\n'):
            continue
        if line.startswith("墩号"):
            inputs = [line.split('=')[-1]]
        if line.startswith("桩号"):
            inputs.append(float(line.split('=')[-1]))

        if line.startswith("左宽"):
            inputs.append(eval(line.split('=')[-1]))

        if line.startswith("总宽"):
            inputs.append(eval(line.split('=')[-1]))

        if line.startswith("A垫石") or line.startswith("B垫石"):
            content = line.split("=")[-1]
            m = content.split('+')
            out = []
            for s in m:
                if "*" in s:
                    for i in range(int(s.split('*')[1])):
                        out.append(float(s.split('*')[0]))
                else:
                    out.append(float(s))
            inputs.append(out)

        if line.startswith("支座距"):
            inputs.append(float(line.split('=')[-1]))
            input_list.append(inputs)

    CB_Group = []
    for inp in input_list:
        tt = CBInfo(inp[0], inp[1], inp[3], inp[2], inp[6], inp[4], inp[5], M)
        CB_Group.append(tt)

    AllDf = pd.DataFrame(columns=['Pier', 'Side', 'PlinthNo', 'East', 'North', ])
    for cb in CB_Group:
        AllDf = AllDf.append(cb.get_dataframe(), ignore_index=True)

    AllDf.to_csv("./%s-%s垫石坐标.csv" % (CB_Group[0].name, CB_Group[-1].name))
    f = 1
